/**
 * Project Name:gc.sms.mvn
 * File Name:EmpolyeeController.java
 * Package Name:com.clps.dev.sms.controller
 * Date:2018-11-28 09:40:30
 * Copyright (c) 2018,http://www.clpsglobal.com/zh/home-cn/ All Rights Reserved.
 *
 */
package com.clps.dev.sms.controller.sm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.clps.dev.sms.common.CommonData;
import com.clps.dev.sms.common.result.ResponseResult;
import com.clps.dev.sms.common.result.ResultState;
import com.clps.dev.sms.common.result.ResultType;
import com.clps.dev.sms.entity.sm.Employee;
import com.clps.dev.sms.service.sm.IEmployeeService;
import com.clps.dev.sms.util.factory.ToolFactory;


/**
 * 
 * ClassName: EmpolyeeController <br/>
 * Description: EmpolyeeController.class
 *
 * @author Jack.Huang
 * @version V1.0
 * @since JDK 1.8
 * @date:2018-11-28 09:40:30
 */
@Controller
@RequestMapping(value = "/emp")
public class EmpolyeeController {

	@Resource
	private IEmployeeService employeeService;
	Logger logger = Logger.getLogger(EmpolyeeController.class);
	
	private ResponseResult<Employee> responseResult = new ResponseResult<>();
	
	@GetMapping(value = "/add")
	public String addEmployee() {
		return "sm/employee_add";
	}

	@ResponseBody
	@PostMapping(value = "/emp/add")
	public ResponseResult<Employee> createEmployee(@RequestBody Employee employee) {
		employee.setEmpCreateTime(ToolFactory.getDateInstance(CommonData.YYYY_MM_DD_HH_MM_SS));
		employee.setEmpPositionId(1);
		employee.setEmpDepartmentId(1);
		employee.setEmpStatus(1);
		employee.setEmpCreateName(1);
		employee.setEmpUpdatedEmployeeName(1);
		responseResult = employeeService.addEmployee(employee);
		if(responseResult.getMessage().equals(ResultType.ADD_SUCCESS)) {
			responseResult.setState(ResultState.SAVESUCCESS);
		}else {
			responseResult.setState(ResultState.SAVEERROR);
		}
		System.out.println(responseResult);
		return responseResult;
	}

	@GetMapping(value = "/list")
	public String showEmployee() {
		return "sm/employee";
	}

	@ResponseBody
	@GetMapping(value="/emp/page")
	public ResponseResult<Employee> getEmployeePageInfo(@RequestParam(value="pageNumber") Integer pageNumber, @RequestParam(value="pageSize") Integer pageSize
			,@RequestParam(value="empName") String empName,@RequestParam(value="empStatus") Integer empStatus,@RequestParam(value="empPosition") Integer empPosition){
		responseResult = employeeService.queryByPageTime(pageNumber, pageSize, CommonData.CREATETIME,empName,empStatus,empPosition);
		responseResult.setState(ResultState.OPERATIONSUCCESS);
		return responseResult;
	}
	@ResponseBody
	@SuppressWarnings("unchecked")
	@GetMapping(value = "/emp")
	public ResponseResult<Employee> getEmployee() {
		responseResult = employeeService.queryAllEmployee();
		Map<String, Object> map = new HashMap<>();
		List<Employee> employees = (List<Employee>) responseResult.getData();
		map.put("employees", employees);
		responseResult.setData(map);
		//responseResult.setPageInfo(null);
		responseResult.setState(ResultState.OPERATIONSUCCESS);
		return responseResult;
	}

	@ResponseBody
	@DeleteMapping(value = "/emp/{empId}/{empStatus}")
	public ResponseResult<Employee> deleteEmployee(
			@PathVariable("empId") String empId,@PathVariable("empStatus") Integer empStatus) {
		int id = Integer.parseInt(empId);
		responseResult = employeeService.deleteEmployee(id,empStatus);
		if (responseResult.getMessage().equals(ResultType.DELETE_SUCCESS)) {
			responseResult.setState(ResultState.REMOVESUCCESS);
		}else {
			responseResult.setState(ResultState.REMOVEERROR);
		}
		return responseResult;
	}

	@GetMapping(value = "/emp/{empId}")
	public String editEmpoyee(@PathVariable("empId") String empId, Model model) {
		int id = Integer.parseInt(empId);
		Employee employee = (Employee) employeeService.queryEmployee(id).getData();
		logger.info(CommonData.QUERY_SUCCESS);
		model.addAttribute("employee", employee);
		return "/sm/employee_edit";
	}

	@ResponseBody
	@PutMapping(value = "/emp/update")
	public ResponseResult<Employee> updateEmployee(@RequestBody Employee employee) {
		employee.setEmpPositionId(1);
		employee.setEmpDepartmentId(1);
		employee.setEmpStatus(1);
		employee.setEmpUpdatedEmployeeName(1);
		responseResult = employeeService.editEmployee(employee);
		if(responseResult.getMessage().equals(ResultType.UPDATE_SUCCESS)) {
			responseResult.setState(ResultState.UPDATE_SUCCESS);
		}else {
			responseResult.setState(ResultState.UPDATE_FAULT);
		}
		return responseResult;
	}
	@ResponseBody
	@GetMapping(value="/emp/check/email/{email}")
	public ResponseResult<Employee> checkUniqueEmail(@PathVariable("email") String email){
		responseResult = employeeService.isUniqueOfEmail(email);
		if(responseResult.getMessage().equals(ResultType.QUERY_FAULT)) {
			responseResult.setState(ResultState.DUPLICATIONERROR);
		}else {
			responseResult.setState(ResultState.QUERYSUCCESS);
		}
		return responseResult;
	}
}
